package com.demo.test.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class HttpClientUtil {

    private CloseableHttpClient httpClient;

    public HttpClientUtil() {
        this.httpClient = HttpClients.createDefault();
    }

    /**
     * GET请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @return 响应结果
     * @throws IOException IO异常
     */
    public String doGet(String url, Map<String, String> headers) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        return executeRequest(httpGet, headers);
    }

    /**
     * POST请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param body    请求体
     * @return 响应结果
     * @throws IOException IO异常
     */
    public String doPost(String url, Map<String, String> headers, String body) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        if (body != null && !body.isEmpty()) {
            StringEntity entity = new StringEntity(body, "UTF-8");
            httpPost.setEntity(entity);
        }
        return executeRequest(httpPost, headers);
    }

    /**
     * PUT请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @param body    请求体
     * @return 响应结果
     * @throws IOException IO异常
     */
    public String doPut(String url, Map<String, String> headers, String body) throws IOException {
        HttpPut httpPut = new HttpPut(url);
        if (body != null && !body.isEmpty()) {
            StringEntity entity = new StringEntity(body, "UTF-8");
            httpPut.setEntity(entity);
        }
        return executeRequest(httpPut, headers);
    }

    /**
     * DELETE请求
     *
     * @param url     请求地址
     * @param headers 请求头
     * @return 响应结果
     * @throws IOException IO异常
     */
    public String doDelete(String url, Map<String, String> headers) throws IOException {
        HttpDelete httpDelete = new HttpDelete(url);
        return executeRequest(httpDelete, headers);
    }

    /**
     * 执行HTTP请求
     *
     * @param request 请求对象
     * @param headers 请求头
     * @return 响应结果
     * @throws IOException IO异常
     */
    private String executeRequest(HttpRequestBase request, Map<String, String> headers) throws IOException {
        // 添加请求头
        if (headers != null) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                request.setHeader(entry.getKey(), entry.getValue());
            }
        }

        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        }
    }
}